/* 
 *  <copyright file="MainMenuScreen.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SobrietyEngine.Assets.Textures;
using SobrietyEngine.Screens;
using SobrietyEngine.Logging;

//
//
//  THIS SCREEN IS UGLY. it is leftover from a week of sleep depraived programming
//  during DBP competition. Its just used to launch otehr screens, so i havent bothere dcleaning this up yet
//  that tiem will come soon enough. Other screens serve as better examples
//
//

namespace SEGameExample.Screens
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the gameServices starts up.
    /// </summary>
    class MainMenu : BaseMenu
    {
        TextureManagerGameService textureManagerGameService;
        //Texture2D logo;
        bool beenDrawn = false;
       // bool freshStart = true;

        public MainMenu()
        {
            MenuEntries.Add(new List<string>(new String[] { "Orientation Test" }));
            MenuEntries.Add(new List<string>(new String[] { "Ugly Platformer Test" }));
            MenuEntries.Add(new List<string>(new String[] { "Model Animation Test" }));
            MenuEntries.Add(new List<string>(new String[] { "Static Geom from large Sketchup Mesh" }));
            MenuEntries.Add(new List<string>(new String[] { "DoomBuilder UDMF map as 2.5d test" }));
            MenuEntries.Add(new List<string>(new String[] { "DoomBuilder UDMF map as 3D FPS test" }));
            MenuEntries.Add(new List<string>(new String[] { "Simple SceneNode test" }));
            MenuEntries.Add(new List<string>(new String[] { "Building actors from script file" }));
            MenuEntries.Add(new List<string>(new String[] { "Exit" }));
            LeftBorder = 100;

            foreach (List<string> l in MenuEntries)
            {
                selectedEntryX.Add(0);
            }
        }


        public override void Initialize()
        {
            base.Initialize();
            textureManagerGameService = (TextureManagerGameService)ScreenManager.Game.Services.GetService(typeof(ITextureManagerGameService));
            //logo = textureManagerGameService.LoadTexture("Menu/MainLogo");
            //soundManager.Play("IntroMusic0", "IntroMusic0");
            EventRouter.LogRaisedEvents = false;

            //lets get sound working?
            soundManager.DoAudio = true;
            soundManager.LoadBank("SEGameExampleContent/Sound/exampleGame.xgs",
                "SEGameExampleContent/Sound/menu.xsb", "SEGameExampleContent/Sound/menu.xwb");

        }

        public override void Show()
        {
            base.Show();
            RegisterEventListeners();
        }

        public override void Hide()
        {
            base.Hide();
            UnregisterEventListeners();
        }

        /// <summary>
        /// Responds to PlayerOne menu selections.
        /// </summary>
        protected override void OnSelectEntry(int entryIndex)
        {
            switch (entryIndex)
            {
                case 0:
                    soundManager.Play("MenuSelect");
                    ScreenManager.AddScreen(new Screens.OrientationTest());
                    break;
                case 1:
                    soundManager.Play("MenuSelect");
                    ScreenManager.AddScreen(new Screens.UglyPlatformer());
                    break;
                case 2:
                    soundManager.Play("MenuSelect");
                    ScreenManager.AddScreen(new Screens.XModelAnim());
                    break;
                case 3:
                    soundManager.Play("MenuSelect");
                    ScreenManager.AddScreen(new Screens.HugeMeshLevel());
                    break;
                case 4:
                    soundManager.Play("MenuSelect");
                    ScreenManager.AddScreen(new Screens.UDMFTest());
                    break;
                case 5:
                    soundManager.Play("MenuSelect");
                    ScreenManager.AddScreen(new Screens.FPSUDMF());                    
                    break;
                case 6://exit
                    soundManager.Play("MenuSelect");
                    ScreenManager.AddScreen(new Screens.SceneNodeTest());
                    break;
                case 7://exit
                    soundManager.Play("MenuSelect");
                    ScreenManager.AddScreen(new Screens.ScriptBuilderTest());
                    break;
                case 8:
                    soundManager.Play("MenuBack");
                    ScreenManager.Game.Exit();
                    break;

            }
        }


        /// <summary>
        /// When the PlayerOne cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel()
        {
            soundManager.Play("MenuBack");
        }

        /// <summary>
        /// RaiseLogEvent handler for when the PlayerOne selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ExitMessageBoxAccepted(object sender, EventArgs e)
        {
            ScreenManager.Game.Exit();
        }

        override protected void CalculatePosition()
        {
            float totalHeight;
            totalHeight = MenuSpriteFont.MeasureString("T").Y;
            totalHeight *= menuEntries.Count;

            position.Y = ((ScreenManager.ScreenHeight - totalHeight) / 2) + 160;
            position.X = (ScreenManager.ScreenWidth ) / 2;
        }


        public override void Update(GameTime gameTime, Boolean coveredByOtherScreen, IScreen otherScreen)
        {
            base.Update(gameTime, coveredByOtherScreen, otherScreen);
            inputGameService.CheckDevices();
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (!Visible)
                return;

            //ScreenManager.GraphicsDevice.Clear(Color.Black);
            ScreenManager.GraphicsDevice.Viewport = ScreenManager.FullScreenViewport;

            ScreenManager.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            if (true)//(freshStart)
            {

                ScreenManager.GraphicsDevice.Clear(Color.Black);
                //freshStart = false;
            }
            string instructions = String.Empty;
#if XBOX360
            instructions = "Left/Right to change options, A to select, B to go back";
#else
            instructions = "Left/Right to change options, A to select, B to go back\n Xbox 360 Joystick REQUIRED for tests (for now)";
#endif


            //bg
            //ScreenManager.SpriteBatch.Draw(bg, bgPos, Color.White);

            //logo
            //ScreenManager.SpriteBatch.Draw(logo, new Vector2(ScreenManager.ScreenWidth / 2 - logo.Width / 2, 50), Color.White);

            ScreenManager.SpriteBatch.DrawString(ScreenManager.DiagnosticSpriteFont, instructions, new Vector2((float)((ScreenManager.ScreenWidth / 2) - (ScreenManager.DiagnosticSpriteFont.MeasureString(instructions).X * 1.25) / 2), ScreenManager.ScreenHeight - 90), Color.BlanchedAlmond, 0f, new Vector2(0, 0), 1.25f, SpriteEffects.None, 0f);
            //ScreenManager.SpriteBatch.DrawString(ScreenManager.DiagnosticSpriteFont, "Pre-alpha, Build week 2.5", new Vector2((float)((ScreenManager.ScreenWidth / 2) - (ScreenManager.DiagnosticSpriteFont.MeasureString("Pre-alpha, Build week 2.5").X * 1.25) / 2), ScreenManager.ScreenHeight - 65), Color.White, 0f, new Vector2(0, 0), 1.25f, SpriteEffects.None, 0f);
            DrawText(gameTime);
            ScreenManager.SpriteBatch.End();

            if (!beenDrawn)
            {
                CalculatePosition();
                beenDrawn = true;
            }
            
        }


        public void DrawText(GameTime gameTime)
        {


            Vector2 itemPosition = position;

            // Draw each menu entry in turn.

            for (int i = 0; i < menuEntries.Count; i++)
            {
                Color color;
                float scale;
                float scale2 = 1.0f;
                Color color2 = Color.Green;

                if (Active && (i == selectedEntry))
                {
                    //// The selected entry is yellow, and has an animating size.
                    double time = gameTime.TotalGameTime.TotalSeconds;

                    float pulsate = (float)Math.Sin(time * 3) + 1;
                    scale2 = 1 + ((float)Math.Cos(time * 3) + 1) * 0.05f;
                    scale = 1 + pulsate * 0.05f;

                    color = Color.MidnightBlue;
                    if (i == 0 && !Globals.ActiveGameTypes.Instance.ActiveTypes.Contains((Globals.GameTypes)selectedEntryX[0]))
                    {
                        color2 = Color.Red;
                    }
                    else if (i == 1 && selectedEntryX[i] == 1)
                    {
                        color2 = Color.Red;
                    }
                    else if (i == 2 && selectedEntryX[0] == 2)
                    {
                        color2 = Color.Red;
                    }
                    else
                    {
                        color2 = Color.White;
                    }
                }
                else if (i == 0 && !Globals.ActiveGameTypes.Instance.ActiveTypes.Contains((Globals.GameTypes)selectedEntryX[0]))
                {
                    color = Color.Red;
                    scale = 1;
                }
                else if (i == 1 && selectedEntryX[i] == 1)
                {
                    color = Color.Red;
                    scale = 1;
                }
                else if (i == 2 && selectedEntryX[0] == 2)
                {
                    color = Color.Red;
                    color2 = Color.Red;
                    scale = 1;
                }
                else
                {
                    // Other entries are green.
                    color = Color.Green;
                    color2 = Color.Red;
                    scale = 1;
                }

                

                // Modify the alpha to fade text out during transitions.
                color = new Color(color.R, color.G, color.B);//, TransitionAlpha);

                // Draw text, centered on the middle of each line.
                Vector2 origin = new Vector2(0, ScreenManager.MenuSpriteFont.LineSpacing / 2);
                itemPosition.X = position.X - (MenuSpriteFont.MeasureString(menuEntries[i][selectedEntryX[i]]).X / 2);
                ScreenManager.SpriteBatch.DrawString(MenuSpriteFont, menuEntries[i][selectedEntryX[i]],
                                                     itemPosition, color, 0, origin, scale,
                                                     SpriteEffects.None, 0);


                itemPosition.Y += MenuSpriteFont.LineSpacing;
            }
        }
    }
}
